let app = getApp();
let API_URL = app.globalData.API_URL;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        idx: null,
        photoId: 0,
        editId: null,
        showDeletePhoto: false,
        uploadImages: [],

        // 填空
        infoObj: {}
    },

    // 发货人
    sender(e) {
        this.data.infoObj.sender = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 发货地址
    from(e) {
        this.data.infoObj.from = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 发件人电话
    sender_phone(e) {
        this.data.infoObj.sender_phone = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 发件人备注
    sender_remark(e) {
        this.data.infoObj.sender_remark = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 收件地址
    shipping_address(e) {
        this.data.infoObj.shipping_address = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 收件人
    receiver(e){
        this.data.infoObj.receiver = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 收件人电话
    shipping_phone(e) {
        this.data.infoObj.shipping_phone = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取_收货人
    receiver_2(e) {
        this.data.infoObj.receiver_2 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取_收货地址
    shipping_address_2(e) {
        this.data.infoObj.shipping_address_2 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取_收件人手机号
    phoneNum_2(e) {
        this.data.infoObj.phoneNum_2 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取_备注
    remark_2(e) {
        this.data.infoObj.remark_2 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    receiver_3(e) {
        this.data.infoObj.receiver_3 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取收件人
    shipping_address_3(e) {
        this.data.infoObj.shipping_address_3 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 代取_送件人电话
    sender_phone_3(e) {
        this.data.infoObj.sender_phone_3 = e.detail.value
        this.setData({ infoObj: this.data.infoObj })
    },

    // 添加照片
    uploadImage() {
        let that = this;
        let imgArr = this.data.uploadImages;
        wx.chooseImage({
            count: 3,
            success(res) {
                const tempFilePaths = res.tempFilePaths
                for (const item of tempFilePaths) {
                    if ( imgArr.length == 3 ) {
                        console.log('已经 3 个照片了');
                        break
                    }
                    let data = {}
                    data.fullUrl = item
                    imgArr.push(data)
                }
                that.setData({
                    uploadImages: imgArr
                })
            }
        })
    },

    // 移除图片
    destroyPhoto(e) {
        let photoId = 0;
        if (e.currentTarget.dataset.item.id) photoId = e.currentTarget.dataset.item.id
        let delIndexPhoto = e.currentTarget.dataset.index
        this.setData({ photoId: photoId, delIndexPhoto: delIndexPhoto, showDeletePhoto: true })
    },

    // 确定删除编辑照片
    editPhotoOnConfirm() {
        if ( this.data.editId !== null ) {
            let data = { fileId: this.data.photoId }
            wx.request({
                url:`${API_URL}/getDeliveryFilesId`,
                header: app.header(),
                data: data,
                method: 'post',
                success: function (res) {}
            })
            let arr = this.data.uploadImages
            arr.splice(this.data.delIndexPhoto, 1)
            this.sendBTN()
            this.setData({ uploadImages: arr, showDeletePhoto: false })
        } else {
            let arr = this.data.uploadImages
            arr.splice(this.data.delIndexPhoto, 1)
            this.setData({ uploadImages: arr, showDeletePhoto: false })
        }
    },

    // 保存
    sendBTN() {
        let idx = this.data.idx
        this.data.infoObj['idx'] = idx;
        if(this.data.uploadImages.length > 0) {
            this.data.infoObj['images'] = this.data.uploadImages;
        }

        var pages = getCurrentPages();   //当前页面

        var prevPage = pages[pages.length - 2];   //上一页面

        let item = this.data.infoObj;
        let json = JSON.stringify(item);
        prevPage.setData({
            //直接给上一个页面赋值
            infoObj: json,
        });

        wx.navigateBack({
            //返回
            delta: 1
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let value = {};
        if(options.json.length > 9) {
            value = JSON.parse(options.json);
        }
        this.setData({ lang: app.globalData.lang, navTitle: "detailed", navigation: true, idx: options.idx, infoObj: value })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})